//
//  ATBReFundListViewController.m
//  quxiangtao
//
//  Created by E人一铺 on 2019/3/20.
//  Copyright © 2019年 shanhaijin. All rights reserved.
//

#import "ATBReFundListViewController.h"
#import "BuyOrderTableViewCell.h"
#import "ATBReFundListModel.h"

static NSString *buyGoodsCellID = @"BuyOrderTableViewCell";
@interface ATBReFundListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *mainTableView;
@property (assign, nonatomic) NSInteger page;

@property (strong, nonatomic) NSMutableArray *orderArr;

@end

@implementation ATBReFundListViewController
#pragma mark -- Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WhiteColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.page = 1;

    [self creatConstraint];
//    [self loadData];
}

#pragma mark -- Constraint
-(void)creatConstraint{
    [self.view addSubview:self.mainTableView];
    __weak typeof(self) weakSelf = self;
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
    }];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [header beginRefreshing];
    [header setTitle:@"下拉可以刷新!" forState:MJRefreshStateIdle];
    [header setTitle:@"松手立即刷新!" forState:MJRefreshStatePulling];
    [header setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    header.stateLabel.font = Font(24);
    header.backgroundColor = [UIColor clearColor];
    [header setTintColor:[UIColor clearColor]];
    
    header.stateLabel.textColor = Color999999;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.mainTableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载。。。。" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据！" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = Font(17);
    footer.stateLabel.textColor = Color999999;
    self.mainTableView.mj_footer = footer;
    self.mainTableView.mj_footer.hidden = NO;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goodsConfirmReceipt:) name:@"ATBRefundListNotifier" object:nil];

    
}

#pragma mark -- LoadData
-(void)loadData{
    self.mainTableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"noData"
                                                                  titleStr:@"暂无数据"
                                                                 detailStr:@""];

    __weak typeof(self) weakSelf = self;
    
    [ATBReFundListModel refundListRequestWithPage:self.page successBlock:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 1) {
            NSArray *arr = responseObject[@"data"];
            if (arr.count > 0 || weakSelf.orderArr.count > 0 ) {
                if (self.page == 1) {
                    
                    weakSelf.orderArr = (NSMutableArray *)arr;
                    
                }else {
                    
                    [weakSelf.orderArr addObjectsFromArray:arr];
                    
                }
                
                [weakSelf.mainTableView reloadData];
                
                if (self.page > 1 && arr.count == 0) {
                    
                    [self.mainTableView.mj_footer endRefreshingWithNoMoreData];
                }
                
                // 滚动cell
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:(self.page - 1) * 10];
                
                [weakSelf.mainTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                
            }
            
        }else  {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
        }
        
        
    } failBlock:^(NSError *error) {
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
        
    }];
    
}
- (void)loadNewData
{
    
    self.page = 1;
    
    [self.mainTableView.mj_footer resetNoMoreData];
    [self loadData];
    
    [self.mainTableView reloadData];
    
    // 拿到当前的下拉刷新控件，结束刷新状态
    [self.mainTableView.mj_header endRefreshing];
    
}

- (void)loadMoreData {
    
    self.page ++;
    
    [self loadData];
    // 刷新表格
    [self.mainTableView reloadData];
    // 拿到当前的上拉刷新控件，结束刷新状态
    [self.mainTableView.mj_footer endRefreshing];
    
}

#pragma mark -- Delegate DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.orderArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BuyOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:buyGoodsCellID];
    
    if (!cell) {
        
        cell = [[BuyOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:buyGoodsCellID];
        
    }
    
    cell.parentVC = self;
    
//    cell.type = [self.tradeType integerValue];
    
//    cell.orderModel = self.orderArr[indexPath.row];
    cell.refundModel = self.orderArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(370);
    
}
#pragma mark -- Event
- (void)goodsConfirmReceipt:(NSNotification *)notification {
    
    NSDictionary *dict = notification.object;
    
    [self.orderArr removeObject:dict[@"model"]];
    
    [self.mainTableView reloadData];
    
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark -- Lazy
- (UITableView *)mainTableView {
    
    if (!_mainTableView) {
        
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        
        _mainTableView.estimatedSectionHeaderHeight = 0;
        _mainTableView.estimatedSectionFooterHeight = 0;
        _mainTableView.estimatedRowHeight = 0;
        
        if (@available(iOS 11.0, *)) {
            
            _mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _mainTableView;
}
-(NSMutableArray *)orderArr{
    if (!_orderArr) {
        _orderArr = [[NSMutableArray alloc]init];
    }
    return _orderArr;
}

@end
